<?php

require_once 'Zend/Mail.php';

class Tavs_Mail_Loader extends Zend_Mail
{
	/**
	 * Namespaces a serem procurados os loaders
	 *
	 * @var array
	 */
	protected static $_namespaces = array('Tavs_Mail_Loader_');

	/**
	 * Cria instancia de Tavs_Mail_Template_Abstract.
	 *
	 * @param string $loader Loader a ser instanciado
	 *
	 * @return Tavs_Mail_Loader_Abstract
	 */
	static public function factory($loader)
	{
		$loader = (string) $loader;

		$found = false;
		$namespaces = self::$_namespaces;
		foreach ($namespaces as $namespace) {
			$className = $namespace . ucfirst($loader);
			try {
				@Zend_Loader::loadClass($className);
				$found = true;
				break;
			} catch (Zend_Exception $e) {}
		}

		if ($found) {
			$loader = new $className();

			if ($loader instanceof Tavs_Mail_Loader_Abstract) {
				return $loader;
			} else {
				throw new Tavs_Mail_Exception('loader must be instance of Tavs_Mail_Loader_Abstract');
			}
		} else {
			throw new Tavs_Mail_Exception($loader . ' not be found in namespaces: ' . implode(PATH_SEPARATOR, $namespaces));
		}
	}
}